<html>
<head><meta charset="utf-8"><title>Deduplication of spans · t-compiler/wg-mir-opt · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/index.html">t-compiler/wg-mir-opt</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication.20of.20spans.html">Deduplication of spans</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="221698463"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication%20of%20spans/near/221698463" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Vandel Sillesen <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication.20of.20spans.html#221698463">(Jan 05 2021 at 20:38)</a>:</h4>
<p>Hi <span class="user-mention" data-user-id="124288">@oli</span><br>
I'm not really clear on what you are suggesting that is done regarding <a href="https://github.com/rust-lang/rust/pull/77551#issuecomment-753639923">https://github.com/rust-lang/rust/pull/77551#issuecomment-753639923</a></p>
<p>So create a new ExpansionKind variant "Deduplicated"? And that variant would then contain two spans? 5 statements in their own bbs could be duplicated though, so maybe a list of spans?</p>
<p>Then when rendering the span for an error, some code would determine which span to present to the user. Can you give an example of the case where a common span could be chosen?</p>



<a name="221699454"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication%20of%20spans/near/221699454" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication.20of.20spans.html#221699454">(Jan 05 2021 at 20:48)</a>:</h4>
<p>Ah no, this is <em>per</em> span. So each deduplication would be a base-span with an expansion containing the other span</p>



<a name="221699489"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication%20of%20spans/near/221699489" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication.20of.20spans.html#221699489">(Jan 05 2021 at 20:48)</a>:</h4>
<p>I'll try to come up with an example tomorrow</p>



<a name="221750330"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication%20of%20spans/near/221750330" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication.20of.20spans.html#221750330">(Jan 06 2021 at 10:05)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">match</span><span class="w"> </span><span class="n">foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="mi">0</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">SOME_CONST</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="mi">1</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">SOME_CONST</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="mi">42</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>The two <code>SOME_CONST</code> arms could be merged, but they have different spans. Thus any diagnostic that would be reported (e.g. the constant failing to evaluate), so instead of</p>
<div class="codehilite"><pre><span></span><code>error[E0080]: erroneous constant used
 --&gt; src/main.rs:7:14
  |
7 |         0 =&gt; SOME_CONST,
  |              ^^^^^^^^^^ referenced constant has errors

error[E0080]: erroneous constant used
 --&gt; src/main.rs:8:14
  |
8 |         1 =&gt; SOME_CONST,
  |              ^^^^^^^^^^ referenced constant has errors

error: aborting due to 2 previous errors
</code></pre></div>
<p>Like what we get now (see <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=8182d6102c1c9110d223e85ec7295a49">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=8182d6102c1c9110d223e85ec7295a49</a>)</p>
<p>we could have</p>
<div class="codehilite"><pre><span></span><code>error[E0080]: erroneous constant used
 --&gt; src/main.rs:7:14
  |
7 |         0 =&gt; SOME_CONST,
  |              ^^^^^^^^^^ referenced constant has errors
8 |         1 =&gt; SOME_CONST,
  |              ^^^^^^^^^^ error also occurring here, due to optimizations
</code></pre></div>
<p>or something... idk, I'm not sure on the details</p>



<a name="221753766"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication%20of%20spans/near/221753766" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication.20of.20spans.html#221753766">(Jan 06 2021 at 10:46)</a>:</h4>
<p>Wouldn't debug info be a better example?</p>



<a name="221753958"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication%20of%20spans/near/221753958" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication.20of.20spans.html#221753958">(Jan 06 2021 at 10:49)</a>:</h4>
<p>Though I guess we are not required to preserve equivalence there.</p>



<a name="221754450"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication%20of%20spans/near/221754450" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication.20of.20spans.html#221754450">(Jan 06 2021 at 10:56)</a>:</h4>
<p>With that in mind it seems to me that running dedup late enough would be sufficient.</p>



<a name="221754487"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication%20of%20spans/near/221754487" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication.20of.20spans.html#221754487">(Jan 06 2021 at 10:57)</a>:</h4>
<p>Late enough that we don't emit diagnostics anymore</p>



<a name="221756693"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication%20of%20spans/near/221756693" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication.20of.20spans.html#221756693">(Jan 06 2021 at 11:25)</a>:</h4>
<p><span class="user-mention silent" data-user-id="123586">nagisa</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/Deduplication.20of.20spans/near/221754487">said</a>:</p>
<blockquote>
<p>Late enough that we don't emit diagnostics anymore</p>
</blockquote>
<p><span aria-label="grinning" class="emoji emoji-1f600" role="img" title="grinning">:grinning:</span> ok, maybe we should have this discussion before adding more span preservation during optimizations, because I think we should have more diagnostics on optimized MIR, not less</p>



<a name="221756715"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication%20of%20spans/near/221756715" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication.20of.20spans.html#221756715">(Jan 06 2021 at 11:25)</a>:</h4>
<p><span class="user-mention silent" data-user-id="123586">nagisa</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/Deduplication.20of.20spans/near/221753766">said</a>:</p>
<blockquote>
<p>Wouldn't debug info be a better example?</p>
</blockquote>
<p>Right, that's more important, so let's focus on that for now</p>



<a name="221796359"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication%20of%20spans/near/221796359" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Vandel Sillesen <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication.20of.20spans.html#221796359">(Jan 06 2021 at 17:31)</a>:</h4>
<p>So are you saying that if we do the deduplication pass late in the pipeline (eg just before codegen), we won't have to worry about diagnostics since those have already been emitted. Then the only worry is debuginfo. But since preserving debuginfo during optimizations is not guaranteed, we could implement the deduplication pass without trying to handle spans correctly?</p>



<a name="221811396"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication%20of%20spans/near/221811396" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication.20of.20spans.html#221811396">(Jan 06 2021 at 19:31)</a>:</h4>
<p>Yeah. Though absolute ordering constraints do make me somewhat uncomfortable</p>



<a name="221811554"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication%20of%20spans/near/221811554" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication.20of.20spans.html#221811554">(Jan 06 2021 at 19:32)</a>:</h4>
<p>Plus not running such a pass early also means that other mir opts don't benefit from having less mir to go through</p>



<a name="221820059"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication%20of%20spans/near/221820059" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Vandel Sillesen <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication.20of.20spans.html#221820059">(Jan 06 2021 at 20:38)</a>:</h4>
<p>What is the latest pass that can emit diagnostics? ConstProp? It could be just after that then.</p>
<p>Would a regression test showing we don't mess up diagnostics for SOME_CONST ala the one oli wrote further up provide comfort?</p>



<a name="221820621"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication%20of%20spans/near/221820621" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication.20of.20spans.html#221820621">(Jan 06 2021 at 20:42)</a>:</h4>
<p>There's no limitation currently</p>



<a name="221820723"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication%20of%20spans/near/221820723" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication.20of.20spans.html#221820723">(Jan 06 2021 at 20:43)</a>:</h4>
<p>Any pass can add diagnostics in the future and anybody can add new passes near end too</p>



<a name="221822094"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication%20of%20spans/near/221822094" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Vandel Sillesen <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication.20of.20spans.html#221822094">(Jan 06 2021 at 20:54)</a>:</h4>
<p>Brainstorming: have each pass that wants to emit diagnostics explicitly register itself. Then only run optimization passes that alter spans if no passes are yet to run that have registered interest in emitting diagnostics</p>



<a name="221844351"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication%20of%20spans/near/221844351" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication.20of.20spans.html#221844351">(Jan 07 2021 at 00:50)</a>:</h4>
<p>This would help with not running lints when cargo sets --cap-lints allow</p>



<a name="221844403"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication%20of%20spans/near/221844403" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Deduplication.20of.20spans.html#221844403">(Jan 07 2021 at 00:51)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/issues/74704">https://github.com/rust-lang/rust/issues/74704</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>